package com.exp.bookhouse.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exp.bookhouse.pojo.Permission;
import com.exp.bookhouse.pojo.Role;
import com.exp.bookhouse.service.RoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "角色管理接口",tags = "管理员管理角色操作")
@Controller
@RequestMapping("/admin")
public class RoleControllerAdmin {

    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "分页查询角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "p", value = "页数", required = false, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "s", value = "显示记录数", required = false, dataType = "Int"),
    })
    @GetMapping("/toRole")
    public String toRole(@RequestParam(value = "p",required = false,defaultValue = "1")Integer page,
                         @RequestParam(value = "s",required = false,defaultValue ="5")Integer size,
                         Model model){
        IPage<Role> iPage =  roleService.queryRoleByPage(page,size);
        model.addAttribute("iPage",iPage);
        return "role";
    }

    @ApiOperation(value = "查询角色可以添加的资源权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色id", required = true, dataType = "Long")
    })
    @GetMapping("/toRoleAddPermission/{id}")
    public String toRoleAddPermission(@PathVariable("id")Long roleId, Model model){
        List<Permission> permissions = roleService.queryOtherPermissions(roleId);
        model.addAttribute("roleId",roleId);
        model.addAttribute("permissions",permissions);
        return "roleAddPermission";
    }


    @ApiOperation(value = "添加用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "roleId", value = "用户id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "form", name = "ids", value = "权限id", required = true, allowMultiple = true, dataType = "String")
    })
    @PostMapping("/saveRoleAddPermission")
    public String saveRoleAddPermission(@RequestParam("roleId")Long roleId, @RequestParam("ids")String[] permissionIds){
        roleService.saveRoleAddPermission(roleId,permissionIds);
        return "redirect:/admin/toRole";
    }


    @GetMapping("/toSaveRole")
    public String toSaveRole(){
        return "roleAdd";
    }


    @ApiOperation(value = "新增角色")
    @PostMapping("/saveRole")
    public String saveRole(@ApiParam(value = "角色信息",required = true) Role role){
        int count = roleService.saveRole(role);
        return "redirect:/admin/toRole";
    }



    @ApiOperation(value = "查询角色详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色id", required = false, dataType = "Long")
    })
    @GetMapping("/queryRoleDetail/{id}")
    public String queryRoleDetail(@PathVariable("id")Long id, Model model){
        Role role = roleService.queryRoleById(id);
        model.addAttribute("role",role);
        return "roleDetail";
    }

    @ApiOperation(value = "根据id查询角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色id", required = false, dataType = "Long")
    })
    @GetMapping("/toUpdateRole/{id}")
    public String toUpdateRole(@PathVariable("id")Long id, Model model){
        Role role = roleService.queryRoleById(id);
        model.addAttribute("role",role);
        return "roleUpdate";
    }


    @ApiOperation(value = "更新角色")
    @PostMapping("/updateRole")
    public String updatePermission(@ApiParam(value = "角色信息",required = true) Role role){
        int count = roleService.updateRole(role);
        return "redirect:/admin/toRole";
    }


    @ApiOperation(value = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "角色id", required = true, dataType = "Long")
    })
    @GetMapping("/deleteRole/{id}")
    public String deleteRole(@PathVariable("id")Long id){
        roleService.deleteRoleById(id);
        return "redirect:/admin/toRole";
    }

}
